﻿#include "verifywidget.h"
#include "ui_verifywidget.h"
#include "chatsocket.h"
#include <QMessageBox>
#include "mylistwidgetitem2.h"
#include "friendrequestlist.h"

VerifyWidget::VerifyWidget(QWidget *parent) :
    MyWidget(parent),
    ui(new Ui::VerifyWidget)
{
    ui->setupUi(this);
    //加载logo图标
    ui->label_icon->setPixmap(QPixmap("res/Icon/joker2.png"));
    setShadow(ui->widget);
    ui->textEdit->setText("我是");
}

VerifyWidget::~VerifyWidget()
{
    delete ui;
}

void VerifyWidget::initAllInfo(const UserInfo &userInfo)
{
    m_userInfo = userInfo;
    ui->label_title->setText("添加好友-" + userInfo.nickname);
    QString path = QCoreApplication::applicationDirPath();
    QString realHeadPath = userInfo.headurl;
    ui->label_head->setStyleSheet(QString("min-width:120px;max-width:120px;min-height:120px;max-height: 120px;border-radius: 60px;border-width: 1 1 1 1;border-image: url(%1) 0 0 0 0 stretch strectch;").arg(realHeadPath));
    ui->label_Id_name->setText(userInfo.id+"\n"+userInfo.nickname);
    ui->label_otherinfo->setText("性别: "+userInfo.sex + "\n年龄: " + userInfo.age);
}

void VerifyWidget::on_btn_close_clicked()
{
    close();
}

void VerifyWidget::on_btn_min_clicked()
{
    showMinimized();
}

void VerifyWidget::on_btn_close2_clicked()
{
    close();
}

void VerifyWidget::on_btn_send_clicked()
{
    QString text = ui->textEdit->toPlainText();
    Json::Value root;
    root["TYPE"] = ADD_FRIEND;
    root["Receiver"] = m_userInfo.id.toStdString();
    root["Sender"] = ChatSocket::getInstance()->getUserID().toStdString();
    root["MSG"] = text.toStdString();
    ChatSocket::getInstance()->sendChatData(root);
//    QMessageBox::information(this,"提示","好友请求已发送,可在请求列表中查看");
    //放入好友列表
    MyListWidgetItem2* mwi = new MyListWidgetItem2(this);
    mwi->initAll(m_userInfo,text);
    mwi->setLableStuats(2);
    FriendRequestList::getInstance()->addItem(mwi);
    //写入文件
    QString path;
    QString id = ChatSocket::getInstance()->getUserID();
    QString str = QCoreApplication::applicationDirPath();
    path = QString("%1/%2/friendRequest.json").arg(str).arg(id);
    FILE* fp = NULL;
    fp = fopen(path.toStdString().c_str(),"r");
    //格式化信息
    root["NickName"] = m_userInfo.nickname.toStdString();
    root["Sex"] = m_userInfo.sex.toStdString();
    root["Age"] = m_userInfo.age.toStdString();
    root["HeadUrl"]= m_userInfo.headurl.toStdString();
    root["PerSig"]= m_userInfo.persig.toStdString();
    root["Praise"] = m_userInfo.praise.toStdString();
    root["BirthDay"] = m_userInfo.birthday.toStdString();
    root["Star"] = m_userInfo.star.toStdString();
    root["Address"] = m_userInfo.address.toStdString();
    root["Phone"] = m_userInfo.phone.toStdString();
    root["Rubric"] = m_userInfo.rubric.toStdString();
    root["Zodiac"] = m_userInfo.zodiac.toStdString();
    root["Status" ] = 2;
    if(fp == NULL)
    {
        fp = fopen(path.toStdString().c_str(),"w");
        if(fp)
        {
            //同步到文件
            Json::Value temp;
            temp.append(root);
            std::string content = temp.toStyledString();
            fwrite(content.c_str(),strlen(content.c_str()),1,fp);
            fflush(fp);
            fclose(fp);
            goto result1;
        }
    }
    else
    {
        fseek(fp, 0, SEEK_END);
        int len = ftell(fp);
        fseek(fp, 0, SEEK_SET);
        char* buff = new char[len + 1];
        fread(buff, len, 1, fp);
        buff[len] = 0;
        Json::Reader reader;
        Json::Value temp;
        if (reader.parse(buff, temp))
        {
            fclose(fp);
            fp = NULL;
            fp = fopen(path.toStdString().c_str(), "w");
            if (fp)
            {
                temp.append(root);
                std::string content = temp.toStyledString();
                int len = strlen(content.c_str());
                fwrite(content.c_str(), len, 1, fp);
                fflush(fp);
                fclose(fp);
            }
        }
        delete [] buff;
        buff = nullptr;
    }
    result1:
    QMessageBox::information(this,"提示","好友请求已发送,可在请求列表中查看");
    close();
}
